// 监听网络请求
chrome.webRequest.onCompleted.addListener(
  async (details) => {
    const { rules = [] } = await chrome.storage.local.get("rules");
    const matchedRule = rules.find((rule) => {
      try {
        return new RegExp(rule.pattern).test(details.url);
      } catch {
        return false;
      }
    });
    const content = new RegExp(matchedRule.pattern).exec(details.url);
    if (matchedRule) {
      chrome.tabs.sendMessage(details.tabId, {
        type: "resource-loaded",
        data: {
          name: matchedRule.name,
          url: details.url,
          timestamp: new Date().toLocaleTimeString(),
          matchContent: content ? content[0] : "",
        },
      });
    }
  },
  { urls: ["<all_urls>"] }
);
